package memberrewards.dao;

import hackathon1212.EMF;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import memberrewards.entities.AccountAdditionalInfo;
import memberrewards.entities.AccountDetail;
import memberrewards.entities.TierConfig;

public class ServiceDAO {
	
	private ServiceDAO() {
	}

	public static AccountDetail getAccountDetail(String pan) {
		final EntityManager em = EMF.get().createEntityManager();
		try{
			AccountDetail accountDetail= em.find(AccountDetail.class, pan);
			if(accountDetail!=null){
				final String tier=accountDetail.getTier();
				TierConfig tierConfig= getTierDetail(tier);
				accountDetail.setTierConfig(tierConfig);
			}
			return accountDetail;
		} catch (NoResultException e){
			return null;
		}
		catch (IllegalArgumentException iaExc){
			return null;
		}
		finally {
			em.close();
		}

	}
	
	public static boolean updateAccountDetail(AccountDetail accountDetail){
		boolean updateStatus=false;
		final EntityManager em = EMF.get().createEntityManager();
		try{
			em.merge(accountDetail);
			updateStatus=true;
		}
		catch(Exception exc){
			exc.printStackTrace(System.err);
		}
		finally{
			em.close();
		}
		return updateStatus;
	}
	
	public static boolean enrollAccount(AccountDetail accountDetail){
		boolean updateStatus=false;
		final EntityManager em = EMF.get().createEntityManager();
		try{
			em.persist(accountDetail);
			updateStatus=true;
		}
		catch(Exception exc){
			exc.printStackTrace(System.err);
		}
		finally{
			em.close();
		}
		return updateStatus;
	}

	public static TierConfig getTierDetail(String tier) {
		final EntityManager em = EMF.get().createEntityManager();
		try{
			TierConfig tierConfig=em.find(TierConfig.class, tier);
			return tierConfig;
		} catch (NoResultException e){
			return null;
		}
		catch (IllegalArgumentException iaExc){
			return null;
		}
		finally {
			em.close();
		}
	}

	@SuppressWarnings("unchecked")
	public static List<AccountAdditionalInfo> getAccountAdditionalInfos(String pan) {
		List<AccountAdditionalInfo> additionalInfos=null;
		final EntityManager em = EMF.get().createEntityManager();
		try{
			Query query=em.createQuery("select addInfo from AccountAdditionalInfo addInfo where addInfo.pan = :PAN");
			query.setParameter("PAN", pan);
			additionalInfos=query.getResultList();
			return additionalInfos;
		} catch (NoResultException e){
			return null;
		}
		finally {
			em.close();
		}
	}
	
	public static boolean addAccountAdditionalInfo(AccountAdditionalInfo accountAdditionalInfo){
		boolean opStatus=false;
		final EntityManager em = EMF.get().createEntityManager();
		try{
			em.persist(accountAdditionalInfo);
			opStatus=true;
		}
		catch(Exception exc){
			exc.printStackTrace(System.err);
		}
		finally{
			em.close();
		}
		return opStatus;
	}
	
	public static Boolean checkCard(String pan) {
		return getAccountDetail(pan) != null;
	}
	

}